﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Manage.ProjectSecurityDepositPaymentDetails;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Project;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.ManagerPannel

{
    /// <summary>
    /// 项目管理员-保证金支付明细
    /// </summary>
    [Route("/api/v1/manager/projectsecuritydepositpaymentdetails")]
    [ApiController]
    [Authorize(Roles = "Manager")]
    public class ProjectSecurityDepositPaymentDetailsController : ControllerBase
    {
        private readonly ProjectSecurityDepositPaymentDetailsService projectSecurityDepositPaymentDetailsService;
        private readonly ProjectManagerApplicationContext applicationContext;
        private readonly IMapper mapper;

        public ProjectSecurityDepositPaymentDetailsController(ProjectSecurityDepositPaymentDetailsService projectSecurityDepositPaymentDetailsService, ProjectManagerApplicationContext applicationContext, IMapper mapper)
        {
            this.projectSecurityDepositPaymentDetailsService = projectSecurityDepositPaymentDetailsService;
            this.applicationContext = applicationContext;
            this.mapper = mapper;
        }

        /// <summary>
        /// 新增保证金支付明细
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        public ApiResult CreateProjectSecurityDepositPaymentDetails(ProjectSecurityDepositPaymentDetailsEditForm form)
        {
            ProjectSecurityDepositPaymentDetails entity = new();
            mapper.Map(form, entity);
            entity.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            entity.CreateTime = DateTime.Now;
            projectSecurityDepositPaymentDetailsService.Create(entity);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 编辑保证金支付明细
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public ApiResult UpdateProjectSecurityDepositPaymentDetails(int id, ProjectSecurityDepositPaymentDetailsEditForm form)
        {
            ProjectSecurityDepositPaymentDetails? data = projectSecurityDepositPaymentDetailsService.GetProjectSecurityDepositPayment(id);
            if (data == null) return ApiResult.Failure("保证金支付明细不存在");
            mapper.Map(form, data);
            data.Id = id;
            projectSecurityDepositPaymentDetailsService.Update(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 保证金支付明细表单数据
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<ProjectSecurityDepositPaymentDetailsEditForm> GetProjectSecurityDepositPaymentDetails(int id)
        {
            ProjectSecurityDepositPaymentDetails? data = projectSecurityDepositPaymentDetailsService.GetProjectSecurityDepositPayment(id);
            if (data == null)
                return ApiResult<ProjectSecurityDepositPaymentDetailsEditForm>.Failure("保证金支付明细不存在");
            ProjectSecurityDepositPaymentDetailsEditForm form = new ProjectSecurityDepositPaymentDetailsEditForm();
            mapper.Map(data, form);
            return ApiResult<ProjectSecurityDepositPaymentDetailsEditForm>.Success(form);
        }

        /// <summary>
        /// 获取保证金支付明细列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<ProjectSecurityDepositPaymentDetailsDto>> GetProjectSecurityDepositPaymentDetails([FromQuery] ProjectSecurityDepositPaymentDetailsQuery query, int pageNum = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var data = projectSecurityDepositPaymentDetailsService.GetProjectSecurityDepositPayments(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ProjectSecurityDepositPaymentDetailsDto>>.Success(dtos);
        }

        /// <summary>
        /// 删除保证金支付明细
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        public ApiResult Delete(string ids)
        {
            projectSecurityDepositPaymentDetailsService.Deleted(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }
    }
}